﻿using System;
using System.Linq;
using System.Web.Mvc;
using DataAccess.DB;

namespace DuocThaiBinh.Controllers
{
    public class ValidationController : BaseController
    {
        public JsonResult CheckUserExist(string ID, string MaHoaDon)
        {
            using (db_YTeTBEntities db = new db_YTeTBEntities())
            {
                XuatKho existMaHoaDon = db.XuatKhoes.Where(p => p.MaHoaDon.Equals(MaHoaDon)).FirstOrDefault();

                if (ID.Equals("undefined"))// mode new
                {
                    if (existMaHoaDon != null)
                    {
                        return Json(false, JsonRequestBehavior.AllowGet);
                    }
                    else
                    {
                        return Json(true, JsonRequestBehavior.AllowGet);
                    }
                }

                else
                {
                    if (existMaHoaDon != null)
                    {

                        if (existMaHoaDon.ID != Convert.ToInt32(ID))
                        {
                            return Json(false, JsonRequestBehavior.AllowGet);
                        }
                        else
                        {
                            return Json(true, JsonRequestBehavior.AllowGet);
                        }
                    }
                    else
                    {
                        return Json(true, JsonRequestBehavior.AllowGet);
                    }
                }
            }
        }

    }
}
